package com.hup.timer.service.gesture;

import com.hup.timer.service.gesture.TimerWindowGesture.GestureCallback;
import com.hup.utils.commons.hardware.MouseUtil;
import com.hup.utils.commons.hardware.ScreenUtil;
import com.hup.utils.commons.start.LookThemeUtil;
import com.hup.utils.swing.FrameUtil;
import lombok.extern.log4j.Log4j2;

import javax.swing.JLabel;
import javax.swing.JWindow;
import javax.swing.Timer;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Window;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @author hugan
 * @date 2020/4/6
 */
@Log4j2
public class SimpleActionListener extends MouseAdapter {

    public static final float ALPHA_MOUSE_ENTRY = 0.03f;

    private final Window win;
    private final GestureCallback callback;
    private final DisableMenuTooltip tooltip;

    public SimpleActionListener(GestureCallback callback) {
        win = callback.getWindow();
        this.callback = callback;
        tooltip = new DisableMenuTooltip();
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        //鼠标进入变透明
        if (!callback.isShowingMenu()) {
            FrameUtil.setAlpha(win, ALPHA_MOUSE_ENTRY);
            tooltip.start();
        }
    }

    @Override
    public void mouseExited(MouseEvent e) {
        //鼠标移出回复透明度
        FrameUtil.setAlpha(win, callback.getAlpha());
        tooltip.cancelAndHide();
    }

    @Override
    public void mousePressed(MouseEvent e) {
        //鼠标点击,都恢复透明度
        FrameUtil.setAlpha(win, callback.getAlpha());
        tooltip.cancelAndHide();
        if (MouseUtil.isLeftButton(e) && e.getClickCount() == 3) {
            callback.switchDisableWindowMenu();
            tooltip.doShow();
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if (MouseUtil.isInsideComponent(win, e)) {
            if (MouseUtil.isMiddleButton(e)) {
                callback.moveToBootLoc();
            } else if (MouseUtil.isRightButton(e)) {
                if (callback.disableWindowMenu()) return;
                callback.showMenu(e);
            }
        }
    }

    private class DisableMenuTooltip extends JWindow {
        private static final String TIP_DISABLE = "菜单已禁用,三击左键启动菜单";
        private static final String TIP_ENABLE = "三击左键,禁用菜单";
        private JLabel label;
        private Timer tooltipTimer;

        public DisableMenuTooltip() {
            initView();
            tooltipTimer = new Timer(800, e -> {
                doShow();
                tooltipTimer.stop();
            });
        }

        private void initView() {
            label = new JLabel();
            label.setFont(new Font("宋体", Font.PLAIN, (int) (14 * LookThemeUtil.getScreenScale())));
            label.setBorder(new LineBorder(Color.BLACK, 1));
            label.setBackground(Color.WHITE);
            add(label);
            setAlwaysOnTop(true);
            FrameUtil.setAlpha(this, 0.8f);
        }

        private void doShow() {
            label.setText(callback.isDisableWindowMenu() ? TIP_DISABLE : TIP_ENABLE);
            pack();

            Dimension screenSize = ScreenUtil.getScreenSize();
            int x = win.getX() < screenSize.width / 2 ? win.getX() : win.getX() - this.getWidth() + win.getWidth();
            int y = win.getY() < screenSize.height / 2 ? win.getY() + win.getHeight() : win.getY() - this.getHeight();
            setLocation(x, y);
            setVisible(true);
        }

        public void start() {
            tooltipTimer.start();
        }

        public void cancelAndHide() {
            tooltipTimer.stop();
            dispose();
        }
    }

}
